<?php

class OrganizationalUnitObjectRoute extends sfDoctrineRoute
{
  
  /**
   * @var $organizational_unit OrganizationalUnit
   */
  protected $organizational_unit = null;

  public function getOrganizationalUnit ()
  {
    if (is_null($this->organizational_unit))
    {
      $this->organizational_unit = $this->getOrganizationalUnitFromParameters();
    }
    return $this->organizational_unit;
  }

  protected function getOrganizationalUnitFromParameters ()
  {
    $parameters = $this->getParameters();
    return Doctrine_Core::getTable('OrganizationalUnit')->findOneById($parameters['organizational_unit_obj_id']);
  }

  public function generate ($params, $context = array(), $absolute = false)
  {
    //    $table_model = OrganizationalUnitTable::getInstance();
    $organizational_unit = $params['unit'];
    if(is_null($organizational_unit)) {
    	$organizational_unit =$params['sf_subject']->getOrganizationalUnit();
    }
    $params['organizational_unit_obj_id'] = $organizational_unit->getId();
    $params['organizational_unit_obj_slug'] = $organizational_unit->getSlug();
    //    foreach ($params as $k => $v)
    //    {
    //      $count = 0;
    //      $column_name = str_replace('organizational_unit_obj_', $replace, $subject, $count);
    //      if ($count > 0 && $table_model->hasColumn($table_model->getColumnName($column_name)))
    //      {
    //        $this->params[$k] = $organizational_unit->get($column_name);
    //      }
    //    }
    return parent::generate($params, $context, $absolute);
  }
}